package com.hxkj.controller.article;

import com.alibaba.fastjson.JSONObject;
import com.hxkj.domain.po.Column;
import com.hxkj.domain.po.ColumnTree;
import com.hxkj.service.article.ColumnService;
import com.platform.common.domain.exception.CustomException;
import com.platform.common.domain.exception.CustomMessageException;
import com.platform.common.domain.result.ResultDataStruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.concurrent.Callable;

/**
 * @ClassName ColumnController
 * @Description TODO
 * @Author Mr.zheng
 * @Date 2018/6/7 11:57
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/column")
public class ColumnController {

    @Autowired
    ColumnService columnService;

    /**
     * @Description saveColumn 新增文章分类
     **/
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Callable<ResultDataStruct<Column>> saveColumn(@RequestBody Column jsonObject) {
        return () ->{
            ResultDataStruct<Column> resultDataStruct = new ResultDataStruct<>();
            try {
                resultDataStruct.setData(columnService.saveColumn(jsonObject));
                resultDataStruct.setMessage("成功");
                resultDataStruct.setCode(200);
            }catch (CustomException e){
                resultDataStruct.setMessage(e.getMessage());
                resultDataStruct.setCode(e.getCode().getCode());
            }catch (CustomMessageException e){
                resultDataStruct.setMessage(e.getMessage());
                resultDataStruct.setCode(e.getCode());
            }catch (Exception e){
                log.error("新增文章分类出错",e);
                resultDataStruct.setMessage("新增文章分类出错");
                resultDataStruct.setCode(400);
            }
            return resultDataStruct;
        };
    }


    /**
     * @Description getColumnTreeList 获取文章分类的树结构列表
     **/
    @RequestMapping(value = "/getlisttree", method = RequestMethod.POST)
    public Callable<List<ColumnTree>> getColumnTreeList(@ModelAttribute Column column) throws Exception {
        return () -> columnService.getColumnTreeList(column);
    }

    /**
     * @Description getShowColumnTreeList 获取文章分类的树结构下拉框
     **/
    @RequestMapping(value = "/getshowlisttree", method = RequestMethod.POST)
    public Callable<List<ColumnTree>> getShowColumnTreeList(@ModelAttribute Column column) throws Exception {
        return () -> columnService.getShowColumnTreeList(column);
    }


    /**
     * @Description removerColumn 删除文章分类
     **/
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public void removeColumn(@ModelAttribute Column column) throws Exception {
        columnService.removeColumn(column);
    }

    /**
     * @Description modifyColumn 修改文章分类
     **/
    @RequestMapping(value = "/modify", method = RequestMethod.POST)
    public void modifyColumn(@RequestBody JSONObject jsonObject) throws Exception {
        columnService.modifyColumn(jsonObject);
    }

    /**
     * @Description modifySortNum 增加文章分类排序
     **/
    @RequestMapping(value = "/modifysortnum", method = RequestMethod.POST)
    public void modifySortNum(@RequestParam HashMap<String, Object> map) throws Exception {
        columnService.modifySortNum(map);
    }

    /**
     * @return
     * @author chenfangzhou
     * @Description 根据文章的分类获取整个分类结构
     * @Param
     **/
    @RequestMapping(value = "/getTopParentColumn", method = RequestMethod.POST)
    public List<Column> getTopParentColumn(@ModelAttribute Column column) throws Exception {
        return columnService.getTopParentColumn(column);
    }
}
